package com.shujia.flink.window

import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.assigners.{SlidingProcessingTimeWindows, TumblingProcessingTimeWindows}
import org.apache.flink.streaming.api.windowing.time.Time

object Demo1TimeWindow {
  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)

    val kvDS: DataStream[(String, Int)] = linesDS.flatMap(_.split(",")).map((_, 1))

    /**
     * 时间窗口
     * TumblingProcessingTimeWindows: 滚动的处理时间窗口
     * SlidingProcessingTimeWindows: 滑动的处理时间窗口
     * TumblingEventTimeWindows：滚动的事件时间窗口
     * SlidingEventTimeWindows： 滑动的事件时间窗口
     */
    val countDS: DataStream[(String, Int)] = kvDS
      .keyBy(_._1)
      .window(SlidingProcessingTimeWindows.of(Time.seconds(15), Time.seconds(5)))
      .sum(1)

    countDS.print()
    env.execute()

  }
}
